<?php

/**
 * Manage success story
 *
 * @author tuancasi
 */
class Success_storyController extends Controller {

    public function actionIndex() {
	$model = new Story('search');
	$model->unsetAttributes();
	if (isset($_GET['Story']) && $_GET['Story'])
	    $model->attributes = $_GET['Story'];
	$this->render('index', array('model' => $model));
    }

    public function actionAdd() {
	$model = new Story('search');
	if (isset($_POST['Story'])) {
	    $model->attributes = $_POST['Story'];
	    $model->user_id = user()->id;
	    $model->time_created = time();
	    if ($model->validate()) {
		$model->save();
		$this->redirect(url('success_story'));
	    }
	}
	$this->render('add', array('model' => $model));
    }

    public function actionUpdate($id) {
	$model = $this->loadModel($id);
	if (isset($_POST['Story'])) {
	    $model->attributes = $_POST['Story'];
	    if ($model->validate()) {
		$model->save();
		Yii::app()->user->setFlash('success', 'You have updated successfully');
	    }
	}
	$this->render('update', array('model' => $model));
    }

    public function actionDelete($id) {
	$model = $this->loadModel($id);
	$model->delete();
    }

    private function loadModel($id) {
	$model = Story::model()->findByPk($id);
	if ($model) {
	    return $model;
	} else {
	    throw new CHttpException(400, 'The page was not found!');
	}
    }

}
